<template>
  <div class="ArticleList">
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        offset="50"
      >
        <ArticleItem
          v-for="item in list"
          :key="item.art_id"
          :artItem="item"
          @disLikeEV="disLikeFn"
          @reportEV="reportArticleFn"
          @click.native="articleDetailClickFn(item.art_id)"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from "@/components/ArticleItem.vue";
import { getArticleListAPI, disLikeArticleAPI, reportArticleAPI } from "@/api";
import Notify from "@/ui/Notify";

export default {
  name: "ArticleList",
  data() {
    return {
      list: [],
      loading: false, //底部加载状态
      finished: false, //底部加载完成
      theTime: new Date().getTime(),

      isLoading: false, //上拉刷新状态
    };
  },
  props: {
    // list: Array,
    channelID: Number,
  },
  created() {
    this.getArticleListFn();
  },
  methods: {
    //获取文章列表
    async getArticleListFn() {
      const res = await getArticleListAPI({
        channel_id: this.channelID,
        timestamp: this.theTime,
      });
      this.list = [...this.list, ...res.data.data.results];
      this.theTime = res.data.data.pre_timestamp;

      this.loading = false; //一定要设为fasle,否则一直为加载中状态

      if (res.data.data.pre_timestamp === null) {
        this.finished = true;
      }

      //要把上拉状态改为fasle
      this.isLoading = false;
    },
    //底部加载
    onLoad() {
      if (this.list.length > 0) {
        this.getArticleListFn();
      }
    },
    //上来刷新
    onRefresh() {
      this.list = [];
      this.theTime = new Date().getTime();

      this.getArticleListFn();
    },
    //文章 - 不感兴趣
    async disLikeFn(id) {
      await disLikeArticleAPI({ artId: id });

      Notify({ type: "success", message: "反馈成功" });
    },
    //文章 - 举报
    async reportArticleFn(id, type) {
      await reportArticleAPI({
        artId: id,
        type,
      });

      Notify({ type: "success", message: "举报成功" });
    },
    //文章 - 跳转详情
    articleDetailClickFn(id) {
      this.$router.push({
        path: `/detail?art_id=${id}`,
      });
    },
  },
  components: {
    ArticleItem,
  },
};
</script>

<style lang="less" scoped></style>
